package com.hgyc.mom.mes.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.mes.mapper.MesPackageRuleMapper;
import com.hgyc.mom.mes.entity.MesPackageRule;
import com.hgyc.mom.mes.entity.MesPackageRuleMaterial;
import com.hgyc.mom.mes.service.MesPackageRuleMaterialService;
import com.hgyc.mom.mes.entity.MesPackageRuleTier;
import com.hgyc.mom.mes.service.MesPackageRuleTierService;
import com.hgyc.mom.mes.vo.MesPackageRuleVO;
import com.hgyc.mom.mes.service.MesPackageRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import com.hgyc.mom.common.exception.BizException;
import org.springframework.util.CollectionUtils;

/**
 * 包装规则Service业务层实现
 *
 * @author fwj
 * @since 2025-06-30
 */
@Slf4j
@Service
public class MesPackageRuleServiceImpl extends ServiceImpl<MesPackageRuleMapper, MesPackageRule> implements MesPackageRuleService {

    @Autowired
    private MesPackageRuleMaterialService mesPackageRuleMaterialService;
    @Autowired
    private MesPackageRuleTierService mesPackageRuleTierService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePacakageRule(MesPackageRuleVO mesPackageRuleVO) {
        // TODO 其他逻辑
        save(mesPackageRuleVO);

        //新增包装物料信息
        List<MesPackageRuleMaterial> materials = mesPackageRuleVO.getAddMaterials();
        if (!CollectionUtils.isEmpty(materials)) {
            materials.forEach(item -> {
                item.setId(null);
                item.setRuleId(mesPackageRuleVO.getId());
            });
            mesPackageRuleMaterialService.saveBatch(materials);
        }
        //新增包装层级数量
        List<MesPackageRuleTier> tiers = mesPackageRuleVO.getAddTiers();
        if (!CollectionUtils.isEmpty(tiers)) {
            tiers.forEach(item -> {
                item.setId(null);
                item.setRuleId(mesPackageRuleVO.getId());
            });
            mesPackageRuleTierService.saveBatch(tiers);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePacakageRule(MesPackageRuleVO mesPackageRuleVO) {
        // TODO 其他逻辑
        updateById(mesPackageRuleVO);

        //删除包装物料信息
        if (!CollectionUtils.isEmpty(mesPackageRuleVO.getDeleteMaterialIds())) {
            mesPackageRuleMaterialService.removeBatchByIds(mesPackageRuleVO.getDeleteMaterialIds());
        }

        //新增包装物料信息
        List<MesPackageRuleMaterial> materials = mesPackageRuleVO.getAddMaterials();
        if (!CollectionUtils.isEmpty(materials)) {
            materials.forEach(item -> {
                item.setId(null);
                item.setRuleId(mesPackageRuleVO.getId());
            });
            mesPackageRuleMaterialService.saveBatch(materials);
        }

        //更新包装物料信息
        if (!CollectionUtils.isEmpty(mesPackageRuleVO.getUpdateMaterials())) {
            List<MesPackageRuleMaterial> updateMaterials = mesPackageRuleVO.getUpdateMaterials();
            mesPackageRuleMaterialService.updateBatchById(updateMaterials);
        }
        //删除包装层级数量
        if (!CollectionUtils.isEmpty(mesPackageRuleVO.getDeleteTierIds())) {
            mesPackageRuleTierService.removeBatchByIds(mesPackageRuleVO.getDeleteTierIds());
        }

        //新增包装层级数量
        List<MesPackageRuleTier> tiers = mesPackageRuleVO.getAddTiers();
        if (!CollectionUtils.isEmpty(tiers)) {
            tiers.forEach(item -> {
                item.setId(null);
                item.setRuleId(mesPackageRuleVO.getId());
            });
            mesPackageRuleTierService.saveBatch(tiers);
        }

        //更新包装层级数量
        if (!CollectionUtils.isEmpty(mesPackageRuleVO.getUpdateTiers())) {
            List<MesPackageRuleTier> updateTiers = mesPackageRuleVO.getUpdateTiers();
            mesPackageRuleTierService.updateBatchById(updateTiers);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removePacakageRule(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
        //删除包装物料信息
        mesPackageRuleMaterialService.deleteBatchByParentIds(ids);
        //删除包装层级数量
        mesPackageRuleTierService.deleteBatchByParentIds(ids);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<MesPackageRule> mesPackageRuleList, boolean updateSupport) {
        if (mesPackageRuleList == null || mesPackageRuleList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + mesPackageRuleList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + mesPackageRuleList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("包装规则导入失败", e);
            throw new BizException("包装规则导入失败：" + e.getMessage());
        }
    }
}